<template>
	<view class="navbar" @tap.stop="showFloat = false">
		<u-navbar :background="background" :title="title" :title-color="titleColor" :border-bottom="borderBottom"
			:immersive="immersive" :title-bold="true" :is-back="false">
			<view class="navbar-left" slot="left">
				<u-icon :name="backIcon" :size="36" v-if="isBack" @click="goBack"></u-icon>
				<view class="line" v-if="isBack"></view>
				<view class="navbar-lists" @tap.stop="showFloat = !showFloat">
					<u-icon :name="$getImageUri('resource/image/shopapi/uniapp/icon_list.png')" :size="32"></u-icon>
					<view class="navbar-float" v-show="showFloat">
						<router-link v-for="(item, index) in newNavLists" :key="index" :to="item.url"
							:nav-type="item.type" class="float-item">
							<view class="flex">
								<u-icon :name="item.icon" :size="44"></u-icon>
								<text class="m-l-20">{{ item.name }}</text>
							</view>
						</router-link>
					</view>
				</view>
			</view>
		</u-navbar>
		<view class="mask" v-show="showFloat" @touchstart="showFloat = false">

		</view>
	</view>
</template>

<script>
	import Cache from '@/utils/cache'
	export default {
		props: {
			// 导航内容
			title: String,
			titleColor: {
				type: String,
				default: '#000000'
			},
			// 导航的背景颜色
			background: {
				type: Object,
				default: () => ({
					background: '#ffffff'
				})
			},
			// 是否显示底部边框
			borderBottom: {
				type: Boolean,
				default: false
			},
			immersive: {
				type: Boolean,
				default: false
			},
			isBack: {
				type: [Boolean, String],
				default: true
			},
		},
		data() {
			return {
				isIndex: false,
				navLists: [{
					url: '/pages/index/index',
					name: this.$zhTran('首页'),
					icon: this.$getImageUri('resource/image/shopapi/uniapp/icon_home.png'),
					type: 'replaceAll'
				}, {
					url: '/bundle2/pages/goods_search/goods_search',
					name: this.$zhTran('搜索'),
					icon: this.$getImageUri('resource/image/shopapi/uniapp/icon_search.png'),
					type: 'push'
				}, {
					url: '/bundle2/pages/shop_cart/shop_cart',
					name: this.$zhTran('购物车'),
					icon: this.$getImageUri('resource/image/shopapi/uniapp/icon_carts.png'),
					type: 'replaceAll'
				}, {
					url: '/pages/user/user',
					name: this.$zhTran('个人中心'),
					icon: this.$getImageUri('resource/image/shopapi/uniapp/icon_user.png'),
					type: 'replaceAll'
				}],
				showFloat: false
			};
		},
		methods: {
			goBack() {
				if (!this.isIndex) {
					this.$Router.back()
					return
				}
				if (Cache.get('cloud_shop_enabled')) {
					this.$Router.replaceAll('/pages/category/category')
				} else {
					this.$Router.replaceAll('/pages/index/index')
				}
			}
		},
		computed: {
			backIcon() {
				const iconName = this.isIndex ? 'icon_home' : 'icon_back'
				return this.$getImageUri(`resource/image/shopapi/uniapp/${iconName}.png`)
			},
			newNavLists() {
				if (Cache.get('cloud_shop_enabled')) {
					let nlist = [...this.navLists],
						navLists = []
					nlist[0].url = '/pages/category/category'
					for (var i in nlist) {
						if (nlist[i].url != '/bundle2/pages/goods_search/goods_search')
							navLists.push(nlist[i])
					}
					return navLists
				}
				return this.navLists
			}
		},
		created() {
			setTimeout(() => {
				let pages = getCurrentPages();
				if (pages.length == 1) {
					this.isIndex = true
				}
			})

		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-left {
			display: flex;
			padding: 12rpx 25rpx;
			border-radius: 30px;
			background: rgba(255, 255, 255, 0.3);
			border: 1rpx solid rgba(0, 0, 0, 0.1);

			.line {
				width: 1px;
				height: 36rpx;
				background: rgba(0, 0, 0, 0.2);
				margin: 0 25rpx;
			}

			.navbar-lists {
				display: flex;
				justify-content: center;
				position: relative;

				.navbar-float {
					position: absolute;
					top: 40px;
					width: 258rpx;
					padding: 0 24rpx;
					background: #fff;
					border-radius: 14rpx;
					box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.06);

					&::before {
						content: '';
						display: block;
						position: absolute;
						left: 50%;
						width: 0;
						height: 0;
						border: 14rpx solid transparent;
						border-bottom-color: #fff;
						transform: translate(-50%, -100%);
					}

					.float-item {
						padding: 20rpx 0;
						display: flex;
						align-items: center;

						&:not(:last-of-type) {
							border-bottom: 1px solid #e5e5e5;
						}
					}
				}
			}
		}

		.mask {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
		}
	}
</style>